//
//  QRCodeViewController.m
//  QRCodeDemo
//
//  Created by liuyuecheng on 15/6/24.
//  Copyright (c) 2015年 liuyuecheng. All rights reserved.
//

#import "QRCodeViewController.h"
//声明一个声音ID
static SystemSoundID shake_sound_male_id = 0;
@interface QRCodeViewController ()

@end

@implementation QRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //判断当前设备是否是模拟器
    if (!TARGET_IPHONE_SIMULATOR)
    {
        [self setupCamera];
    }
}
- (void)setupCamera
{
    // 取到摄像头设备
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // 初始化输入源
    _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    
    //初始化输出源
    _output = [[AVCaptureMetadataOutput alloc]init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // 初始化会话
    _session = [[AVCaptureSession alloc]init];
    //设置为高清摄像头
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    if ([_session canAddInput:self.input])
    {
        [_session addInput:self.input];
    }
    
    if ([_session canAddOutput:self.output])
    {
        [_session addOutput:self.output];
    }
    
    //设置输出扫描结果   条码类型 AVMetadataObjectTypeQRCode
    _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
    
    // 扫码框
    _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
    //设置扫码框为缩放
    _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    _preview.frame =CGRectMake(16,108,288,220);
    [self.view.layer insertSublayer:self.preview atIndex:0];
    
    // 开始扫描
    [_session startRunning];
}
#pragma mark AVCaptureMetadataOutputObjectsDelegate
//扫码结果回调
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    
    NSString *stringValue;
    
    if ([metadataObjects count] >0)
    {
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
        stringValue = metadataObject.stringValue;
    }
    
    [_session stopRunning];
    
    //取到声音文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"wav"];
    if (path) {
        //注册声音到系统
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&shake_sound_male_id);
    }
    //播放声音
    AudioServicesPlaySystemSound(shake_sound_male_id);
    
    NSLog(@"扫描结果==%@",stringValue);
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:stringValue delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    
//    //这个方法可以打开,电话,短信,邮件,APPstore
//    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"tel://10010"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
